<?php
namespace app\hsycms\model;
use think\Db;
use think\Model;

/**
 * 文章Tag标签
 */
class Taglist extends Model
{	
  
	//添加TAG标签
	public function addTag($aid="",$nid="",$tags="",$act="add",$oldtags=""){
		if($act=="add"){
			if($tags!=""){
				$tagsList = explode('|',$tags);				//分割成数组				
				foreach($tagsList as $key=>$v){
					$v = trim($v);					
					$tagres = db('taglist')->where(['tag'=>$v])->find();
					if(!$tagres){
						$tid = db('taglist')->insertGetId(['tag'=>$v,'datetime'=>time()]);
						db('tagnidaid')->insert(['tid'=>$tid,'nid'=>$nid,'aid'=>$aid]);
					}else{
						$mp['tid'] = db('taglist')->where(['tag'=>$v])->value('id');
						$mp['nid'] = $nid;
						$mp['aid'] = $aid;
						if(!db('tagnidaid')->where($mp)->find()){
							db('tagnidaid')->insert(['tid'=>$mp['tid'],'nid'=>$nid,'aid'=>$aid]);
						}
					}
				}
			}
		}else{
			//旧TAG不为空
			if($oldtags != ""){ 
				$tagsList = explode('|',$oldtags);		//分割成数组				
				foreach($tagsList as $key=>$v){
					$v = trim($v);
					$tid = db('taglist')->where(['tag'=>$v])->value('id');
					$res = db('tagnidaid')->where(['tid'=>$tid,'nid'=>$nid,'aid'=>$aid])->find();
					if($res){
						db('tagnidaid')->where(['tid'=>$tid,'nid'=>$nid,'aid'=>$aid])->delete();
						if(db('tagnidaid')->where(['tid'=>$tid])->count()==0){
							db('taglist')->where(['tag'=>$v])->delete();
						}
					}
				}
			}
			//新TAG不为空 
			if($tags != ""){ 
				$this->addTag($aid,$nid,$tags,$act="add",$oldtags="");
			}			
		}
	}
	
	//删除TAG并更新文章TAG
	public function delTag($tid=""){
		$tag = db('taglist')->where(['id'=>$tid])->value('tag');
		$arclist = db('tagnidaid')->where(['tid'=>$tid])->select();
		foreach($arclist as $key=>$v){
			$tags = db('article')->where(['id'=>$v['aid']])->value('tags');
			if($tags!=""){
				$tagarr = explode('|',$tags);
				foreach($tagarr as $k=>$vs){
					if($vs==$tag){ 
						unset($tagarr[$k]); 
					}
				}
				$newtag = implode('|',$tagarr);
				db('article')->where(['id'=>$v['aid']])->update(['tags'=>$newtag]);
			}
		}
		db('tagnidaid')->where(['tid'=>$tid])->delete();
	}
	
	//文章删除同时删除TAG
	public function delArcTag($aid=""){
		 $res = db('article')->where(['id'=>$aid])->find();
		 if($res['tags']!=""){
			 $this->addTag($aid,$res['nid'],"",$act="edit",$res['tags']);
		 }
	}
	
	//修改文章TAG
	public function editArcTag($tid="",$newtag="",$oldtag=""){
		$arclist = db('tagnidaid')->where(['tid'=>$tid])->select();
		foreach($arclist as $key=>$v){
			$tags = db('article')->where(['id'=>$v['aid']])->value('tags');
			if($tags!=""){
				$tags = str_replace($oldtag,$newtag,$tags);
				db('article')->where(['id'=>$v['aid']])->update(['tags'=>$tags]);
			}
		}
	}
	
	
	//关联文章TAG
	public function glArcTag($tid="",$tag,$oldid="",$newidarr=[]){
		if($oldid!=""){
			//有已关联文章
			$oldidsarr = explode('|',$oldid);				
			$xtidarr = array_values(array_intersect($oldidsarr,$newidarr)); //取出相同值,相同ID不作为				
			$bxtidarr = array_merge(array_diff($oldidsarr,$newidarr),array_diff($newidarr,$oldidsarr)); //取出不相同的值				
			$bxtoldids = array_values(array_intersect($oldidsarr,$bxtidarr)); //不相同时旧ID移除标签
			$bxtnewids = array_values(array_intersect($newidarr,$bxtidarr)); //不相同时新ID添加标签				
			foreach($bxtoldids as $key=>$v){
				//移除旧标签
				$arc = db('article')->field('nid,tags')->where('id',$v)->find();
				$tagarr = explode('|',$arc['tags']);
				foreach($tagarr as $k=>$vs){
					if($vs==$tag){ unset($tagarr[$k]); }
				}
				$newtag = implode('|',$tagarr);
				db('article')->where(['id'=>$v])->update(['tags'=>$newtag]);
				db('tagnidaid')->where(['tid'=>$tid,'nid'=>$arc['nid'],'aid'=>$v])->delete();
			}
			foreach($bxtnewids as $key=>$v){
				//增加新标签
				$arc2 = db('article')->field('nid,tags')->where('id',$v)->find();
				$d['tid'] = $tid;
				$d['nid'] = $arc2['nid'];
				$d['aid'] = $v;
				db('tagnidaid')->insert($d);
				$tags = $arc2['tags'] == "" ? $tag : $tag.'|'.$arc2['tags'];
				db('article')->where(['id'=>$v])->update(['tags'=>$tags]);
			}			
		}else{ 
			//无已关联文章
			foreach($newidarr as $key=>$v){
				$res = db('article')->field("tags,nid")->where('id',$v)->find();
				$d['tid'] = $tid;
				$d['nid'] = $res['nid'];
				$d['aid'] = $v;
				db('tagnidaid')->insert($d);
				$tags = $res['tags'] == "" ? $tag : $tag.'|'.$res['tags'];
				db('article')->where(['id'=>$v])->update(['tags'=>$tags]);
			}
		}
	}
	
}